package com.joe.xl.demo.thread;

import java.util.concurrent.TimeUnit;

public class StopThread {

    public static void main(String[] args) {
        // Thread thread = new Thread(new Count());

        // Thread thread = new Thread(new CountInterrupt());

        // 异常停止法
        Thread thread = new Thread(new ExceptionStopCount());

        // 异常停止法
//        Thread thread = new Thread(new ReturnStopCount());

        //沉睡中停止
//        Thread thread = new Thread(new SleepingStop());

        thread.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();


        // 静态方法 interrupted()。测试当前线程是否已经中断。线程的中断状态由该方法清除为false。 如果连续两次调用，则第二次调用将返回false
        /*
        thread.interrupt();
        System.out.println("is stop1 ? " + Thread.interrupted()); // false
        System.out.println("is stop2 ? " + Thread.interrupted()); //false*/

        /*
        Thread.currentThread().interrupt();
        System.out.println("is stop3 ? " + Thread.interrupted()); // true
        System.out.println("is stop4 ? " + Thread.interrupted()); //false*/

        // 非静态方法 interrupted()。测试线程Thread对象是否已经中断。
        /*
        thread.interrupt();
        System.out.println("is stop5 ? " + thread.isInterrupted()); // true
        System.out.println("is stop6 ? " + thread.isInterrupted()); //true*/

        /*
        Thread.currentThread().interrupt();
        System.out.println("is stop7 ? " + thread.isInterrupted()); // false
        System.out.println("is stop8 ? " + thread.isInterrupted()); //false*/

        // -----------------------------------
    }


    /***
     * interrput()之后，for()继续输出
     */
    static class Count implements Runnable {
        @Override
        public void run() {
            for(int i=0; i<100000; i++){
                System.out.println(i);
            }
        }
    }

    /***
     * for()之后的还会继续打印
     */
    static class CountInterrupt implements Runnable {
        @Override
        public void run() {
            for(int i=0; i<100000; i++){
                System.out.println(i);
                if(Thread.currentThread().interrupted()) {
                    break;
                }
            }

            System.out.println("测试 for()之后是否输出");
        }
    }

    /***
     * 异常停止法，for之后不会打印
     */
    static class ExceptionStopCount implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 100000; i++) {
                    System.out.println(i);
                    if (Thread.currentThread().interrupted()) {
                        throw new InterruptedException();
                    }
                }

                System.out.println("测试 for()之后是否输出");
            } catch (Exception e){
                System.out.println("ExceptionStopCount 异常停止");
                /***
                 * Exception handler
                 */
//                throw new RuntimeException("sys error");
            }
        }
    }

    /***
     * return停止法，for之后不会打印
     */
    static class ReturnStopCount implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 100000; i++) {
                    System.out.println(i);
                    if (Thread.currentThread().interrupted()) {
                        return;
                    }
                }

                System.out.println("测试 for()之后是否输出");
            } catch (Exception e){
                System.out.println("ExceptionStopCount 异常停止");
            }
        }
    }

    /***
     * 睡眠停止法 操作要在睡眠之后，并且interrupt()再睡醒之后，限制较多
     */
    static class SleepingStop implements Runnable {
        @Override
        public void run() {
            System.out.println("SleepingStop begin ...");
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("SleepingStop end ...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
